#include<QPainter>
#include<QApplication>
#include<widget.h>
#include<bars.h>
#include<iostream>

#include<QTimer>
#include<replay.h>

flappyBird::flappyBird(int c_back, int c_bird, int c_bar, QWidget *parent)
    :QWidget (parent)
{
    colorOfBars=c_bar;
    lastPipe=0;
    add_once=true;
    scores=0;
    state=-1;
    bird =new Bird(c_bird);
    bottom=new ground();
    playBtn=new Replay();
    gameoverBoard=new overBoard;
    for(int i=0;i<NUM_B;i++)
    {
        int temp=qrand()%150;
        int temp1=temp;
            bars[i]=new Bars(180+temp1,180+temp1+150,30+i*220+400,c_bar);//管子高度320，间隔150}


    }
    timerId=new QTimer;
    updateTimer=new QTimer;
    dieTimer=new QTimer;
    connect(timerId,&QTimer::timeout,this,&flappyBird::moveObjects);
    connect(timerId,&QTimer::timeout,this,&flappyBird::checkCollison);
    connect(updateTimer,SIGNAL(timeout()),this,SLOT(update()));
    connect(dieTimer,&QTimer::timeout,this,&flappyBird::die);
    scoreBoard =new scoreNum;
    updateTimer->start(100);
    backGround=c_back;

    playList=new QMediaPlaylist;
    QFileInfo info;
    info.setFile("sounds/sfx_wing.mp3");
    playList->addMedia(QUrl::fromLocalFile(info.absoluteFilePath()));
    info.setFile("sounds/sfx_point.mp3");
    playList->addMedia(QUrl::fromLocalFile(info.absoluteFilePath()));
    info.setFile("sounds/sfx_die.mp3");
    playList->addMedia(QUrl::fromLocalFile(info.absoluteFilePath()));
    info.setFile("sounds/sfx_hit.mp3");
    playList->addMedia(QUrl::fromLocalFile(info.absoluteFilePath()));
    info.setFile("sounds/sfx_swooshing.mp3");
    playList->addMedia(QUrl::fromLocalFile(info.absoluteFilePath()));

    playList->setPlaybackMode(QMediaPlaylist::CurrentItemOnce);

    playList_wing=new QMediaPlaylist;
    info.setFile("sounds/sfx_wing.mp3");
    playList_wing->addMedia(QUrl::fromLocalFile(info.absoluteFilePath()));
    playList_wing->setPlaybackMode(QMediaPlaylist::CurrentItemOnce);
    playList_wing->setCurrentIndex(1);

    media=new QMediaPlayer;
    media->setVolume(100);
    media->setPlaylist(playList_wing);

    isFlag=-1;
    media2=new QMediaPlayer;
    media2->setPlaylist(playList);
    media2->setVolume(100);
    isHit=0;

    this->setWindowTitle("Qt FlappyBird");		///设置标题
}

void flappyBird::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    drawObjects(&painter);

}


void flappyBird::keyPressEvent(QKeyEvent *e)
{
      if(e->key()==Qt::Key_Space)
    {
       if(state==-1)
          {
           state=1;
           timerId->start(100);
       }
       if(state==1)
         bird->fly();
     playWingSound();
    }

}

void flappyBird::mousePressEvent(QMouseEvent * e)
{
 if(state==0)
     if(e->button()==Qt::LeftButton)
     {
         int x=e->x();
         int y=e->y();
         if(x>350&&x<350+116)
             if(y>400&&y<400+70)
                {
                 resetGame();
             }
     }
}

void flappyBird::resetGame()
{

    dieTimer->stop();
    state=-1;
    bird->resetState();
    for(int i=0;i<NUM_B;i++)
    {
        int temp1=qrand()%150;
        bars[i]=new Bars(150+temp1,200+temp1+150,30+i*220+400,colorOfBars);//管子高度320，间隔150
    }
    scores=0;
    scoreBoard->setScore(scores);
    playSound(s_sw);
}



void flappyBird::drawObjects(QPainter * painter)
{
    QRect w(0,0,880,640);
    QImage bg;
    if(backGround==0)
         bg.load(":/new/prefix1/flappybird/bg_day.png");
    else {
         bg.load(":/new/prefix1/flappybird/bg_night.png");
    }
    painter->drawImage(w,bg);
    painter->drawImage(bird->getRect(),bird->getImage());
    for(int i=0;i<NUM_B;i++)
    {
      painter->drawImage(bars[i]->getRect(up),bars[i]->getImage(up));
      painter->drawImage(bars[i]->getRect(down),bars[i]->getImage(down));
    }
   painter->drawImage(bottom->getRect(),bottom->getImage());
    //painter->drawImage(scoreBoard->getRect(),scoreBoard->getImage());
    int temp=scoreBoard->getDigit();

    while(temp>=0)
    {

        if(scores==0)
        {
            painter->drawImage(QRect (380,50,24,44),scoreBoard->getZero());

        }
        painter->drawImage(scoreBoard->getRect(temp),scoreBoard->getImage(temp));
        temp--;
    }
    if(state==-1)
    {
        QRect rect(120,130,114,98);
        QImage pic(":/new/prefix1/flappybird/tutorial.png");
        painter->drawImage(rect,pic);
        QRect rect1(100,400,196,62);
        pic.load(":/new/prefix1/flappybird/text_ready.png");
        painter->drawImage(rect1,pic);
        QRect rect2(100,50,178,48);
        pic.load(":/new/prefix1/flappybird/title.png");
        painter->drawImage(rect2,pic);
    }

    if(state==0)
    {
        painter->drawImage(gameoverBoard->getRect(),gameoverBoard->getImage());
        QImage pic(":/new/prefix1/flappybird/tutorial.png");
        painter->drawImage(playBtn->getRect(),playBtn->getImage());
    }
}

void flappyBird::moveObjects()
{
    bird->drop();
   for(int i=0;i<NUM_B;i++)
       bars[i]->move();
}


void flappyBird::stopGame()
{
playSound(s_hit);
 timerId->stop();
 dieTimer->start(100);
}
void flappyBird::checkCollison()
{
    if(bird->getRect().bottom()>540)
        stopGame();
    for(int i=0;i<NUM_B;++i)
    {
        if(bird->getRect().intersects(bars[i]->getRect(up))||bird->getRect().intersects(bars[i]->getRect(down)))
        {
            stopGame();
        }
        if(bird->getRect().intersects(bars[i]->getSpace()))
            if(bars[i]->getSpace().right()<bird->getRect().right()&&add_once)
            {
                scores++;
                playSound(s_point);
                scoreBoard->setScore(scores);
                bird->level=scores%5;
                lastPipe=i;
                add_once=false;
            }
        if(bird->getRect().left()>bars[lastPipe]->getSpace().right())
            add_once=true;
    }

}

void flappyBird::die()
{
    state=0;
 if((bird->getRect().y()+bird->getRect().height())<(bottom->getRect().y()-20))
     bird->drop();
 gameoverBoard->move();
}

void flappyBird::playWingSound()
{
    media->stop();

    media->play();
}

void flappyBird::playSound(int flag)
{
    if(isFlag!=flag)
    {
        playList->setCurrentIndex(flag);
    }
    media2->play();
}


